package com.example.kehoobackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.kehoobackend.domain.Answer;
import com.example.kehoobackend.domain.Collection;
import com.example.kehoobackend.domain.Question;
import com.example.kehoobackend.persistance.CollectionMapper;
import com.example.kehoobackend.service.AnswerService;
import com.example.kehoobackend.service.CollectionService;
import com.example.kehoobackend.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("collectionService")
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {

//    @Autowired
//    QueryWrapper<Collection> queryWrapper;

    @Lazy
    @Autowired
    AnswerService answerService;

    @Lazy
    @Autowired
    QuestionService questionService;


    @Override
    public List<Collection> allCollection(int user_id) {
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Answer> answerQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();

        queryWrapper.eq("user_id",user_id);
        List<Collection> collections = list(queryWrapper);
        for(Collection collection : collections){
            Integer answer_id = collection.getAnswer_id();
            answerQueryWrapper.eq("answer_id",answer_id);
            Answer answer = answerService.getOne(answerQueryWrapper);

            Integer question_id = answer.getQuestion_id();
            questionQueryWrapper.eq("question_id",question_id).select("question_title");
            Question question = questionService.getOne(questionQueryWrapper);


            collection.setQuestion_id(question_id);
            collection.setQuestion_title(question.getQuestion_title());
            collection.setAnswer_content(answer.getAnswer_content());

            answerQueryWrapper.clear();
        }


        return collections;
    }

    @Override
    public Integer countUserCollection(int user_id) {
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user_id);
        return (int) (count(queryWrapper));
    }

    @Override
    public boolean addCollection(int user_id, int answer_id) {
        Collection collection = new Collection();
        collection.setUser_id(user_id);
        collection.setAnswer_id(answer_id);
        return save(collection);
    }

    @Override
    public boolean deleteCollection(int user_id,int answer_id) {
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user_id).eq("answer_id",answer_id);

        return remove(queryWrapper);
    }

    @Override
    public boolean isCollected(Integer answer_id, Integer user_id) {
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("answer_id",answer_id).eq("user_id",user_id);
        Collection collection = getOne(queryWrapper);
        return collection != null;
    }


}
